/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package coordinates;


import static com.googlecode.javacpp.Loader.sizeof;
import static com.googlecode.javacv.cpp.opencv_core.*;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.*;

/**
 *
 * @author Acer
 */
public class BlobMeans {

    IplImage OrgImage, grayImage, result;
    CvMemStorage mem;
    CvSeq contours = new CvSeq();
    CvSeq ptr = new CvSeq();
    CvSeq largestBlob = new CvSeq();


    public IplImage findLargestBlob(IplImage image) {
        
        OrgImage = image;
        grayImage = cvCreateImage(cvGetSize(OrgImage), IPL_DEPTH_8U, 1);
//        cvCvtColor(OrgImage, grayImage, CV_BGR2GRAY);
         cvCanny(OrgImage, OrgImage, 100, 200, 3);
//        cvEqualizeHist(OrgImage, OrgImage);
//        cvThreshold(OrgImage, OrgImage, 100, 255, CV_THRESH_BINARY);

        mem = cvCreateMemStorage(0);

        int a = cvFindContours(OrgImage, mem, contours, sizeof(CvContour.class), CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, cvPoint(0, 0));
        System.out.println("a= "+a);
        int jo = 0;
        int size = 0;
        for (ptr = contours; ptr != null && jo < a; ptr = ptr.h_next()) {
            jo++;
            if (size < ptr.total()) {
                largestBlob = ptr;
                size = ptr.total();
                System.out.println(ptr.total());
            }
        }
         for (ptr = contours; ptr != null && jo < a; ptr = ptr.h_next()) {
            jo++;
            if (size < ptr.total()) {
                largestBlob = ptr;
                size = ptr.total();
                System.out.println(ptr.total());
            }
        }
        System.out.println("Largest= "+largestBlob.total());

        cvDrawContours(OrgImage, largestBlob, CvScalar.RED, CV_RGB(0, 0, 0),-1, CV_FILLED, 8, cvPoint(0, 0));
        return OrgImage;
    }
}
